package com.z.system.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 资源控制器，提供受保护的API端点
 */
@RestController
@RequestMapping("/api/resources")
@Tag(name = "资源管理", description = "受保护的资源访问接口")
public class ResourceController {

    /**
     * 所有认证用户都可以访问的端点
     */
    @GetMapping("/public")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "公开资源", description = "所有认证用户都可以访问的资源",
               responses = @io.swagger.v3.oas.annotations.responses.ApiResponse(
                   responseCode = "200", description = "成功响应",
                   content = @Content(schema = @Schema(implementation = String.class))))
    public String getPublicResource() {
        return "This is a public resource accessible to all authenticated users.";
    }

    /**
     * 需要USER角色才能访问的端点
     */
    @GetMapping("/user")
    @PreAuthorize("hasRole('USER')")
    @Operation(summary = "用户资源", description = "需要USER角色才能访问的资源",
               responses = @io.swagger.v3.oas.annotations.responses.ApiResponse(
                   responseCode = "200", description = "成功响应",
                   content = @Content(schema = @Schema(implementation = String.class))))
    public String getUserResource() {
        return "This is a resource accessible to users with USER role.";
    }

    /**
     * 需要ADMIN角色才能访问的端点
     */
    @GetMapping("/admin")
    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "管理员资源", description = "需要ADMIN角色才能访问的资源",
               responses = @io.swagger.v3.oas.annotations.responses.ApiResponse(
                   responseCode = "200", description = "成功响应",
                   content = @Content(schema = @Schema(implementation = String.class))))
    public String getAdminResource() {
        return "This is a resource accessible to users with ADMIN role.";
    }
}